package com.tiantianyuegou.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.activity.RefundActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016/1/13.
 * 订单详情界面，商品信息的适配器
 */
public class MyGoodsInfoAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private String[] text_details_goods_name;
    private String[] text_details_goods_price;
    private String[] text_details_goods_num;
    private String[] text_details_goods_info;
    private JSONArray products;
    private LayoutInflater inflater;

    public MyGoodsInfoAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public MyGoodsInfoAdapter(Context context, JSONArray products) {
        this.context = context;
        this.products = products;
        inflater = LayoutInflater.from(context);
    }

    public MyGoodsInfoAdapter(Context context, String[] text_details_goods_name, String[] text_details_goods_price, String[] text_details_goods_num, String[] text_details_goods_info) {
        this.context = context;
        this.text_details_goods_name = text_details_goods_name;
        this.text_details_goods_price = text_details_goods_price;
        this.text_details_goods_num = text_details_goods_num;
        this.text_details_goods_info = text_details_goods_info;
        inflater = LayoutInflater.from(context);

    }

    public void Data(JSONArray products) {
        this.products = products;
    }

    @Override
    public int getCount() {
        return products.length();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_my_goods, null);
            holder = new Holder();
            holder.text_goods_name = (TextView) view.findViewById(R.id.text_goods_name);
            holder.text_goods_price = (TextView) view.findViewById(R.id.text_goods_money);
            holder.text_goods_num = (TextView) view.findViewById(R.id.text_goods_num);
            holder.text_goods_info = (TextView) view.findViewById(R.id.text_goods_info);
            holder.text_goods_state = (TextView) view.findViewById(R.id.text_goods_state);
            holder.btn_tuikuan = (Button) view.findViewById(R.id.btn_tuikuan);
            holder.img_goods_picture = (ImageView) view.findViewById(R.id.img_goods_picture);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }


//        holder.text_goods_name.setText(text_details_goods_name[i]);
//        holder.text_goods_price.setText(text_details_goods_price[i]);
//        holder.text_goods_num.setText(text_details_goods_num[i]);
//        holder.text_goods_info.setText(text_details_goods_info[i]);

        try {
            JSONObject goods;
            // for (int s=0;s<products.length();s++){
            goods = null;
            goods = products.getJSONObject(i);

            holder.text_goods_name.setText(goods.getString("tpName"));
            Double price= Double.valueOf(goods.getString("todPrice"));
            holder.text_goods_price.setText("￥" +new DecimalFormat("0.00").format(price));
            holder.text_goods_num.setText("x" + goods.getString("todCount"));
            String pic = goods.get("tpPic").toString();
            if (pic.contains(",")) {
                pic = pic.substring(0, pic.indexOf(","));
            }
            ImageLoader.getInstance().displayImage(pic, holder.img_goods_picture);
            String spec = goods.getString("todSpecJson");
            if (spec.contains("[")) {
                String strInfo = new String();
                //JSONObject goodsOb = jsonGoods.getJSONObject("todSpecJson");

                try {
                    JSONArray goodsInfo = new JSONArray(goods.getString("todSpecJson"));


                    Log.e("adapter", goodsInfo.toString());
                    for (int info = 0; info < goodsInfo.length(); info++) {
                        JSONObject objectInfo = goodsInfo.getJSONObject(info);
                        strInfo += (objectInfo.getString("tsName") + ":");
                        strInfo += (objectInfo.getString("value"));
                        strInfo += ("\t\t");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("adapter", "strInfo    " + strInfo);
                ((TextView) view.findViewById(R.id.text_goods_info)).setText(strInfo);

            } else {
                ((TextView) view.findViewById(R.id.text_goods_info)).setText("");

            }
            //         }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        holder.text_goods_state.setVisibility(View.GONE);
        holder.btn_tuikuan.setVisibility(View.GONE);

        holder.btn_tuikuan.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_tuikuan:
                Intent btn_tuikuan = new Intent();
                btn_tuikuan.setClass(context, RefundActivity.class);
                context.startActivity(btn_tuikuan);
                break;
            default:
                break;
        }
    }

    public static class Holder {
        TextView text_goods_name, text_goods_price, text_goods_num, text_goods_info, text_goods_state;
        Button btn_tuikuan;
        ImageView img_goods_picture;
    }
}
